//
//  SYJWXWarnView.m
//  JieJieKan
//
//  Created by 极速现金侠 on 2017/7/12.
//  Copyright © 2017年 innext. All rights reserved.
//

#import "SYJWXWarnView.h"

@interface SYJWXWarnView ()
@property (nonatomic , weak) UIView *backView;
@property (nonatomic , weak) UIImageView *backImageView;
@property (nonatomic , weak) UIButton *canncelButton;
@property (nonatomic , strong) NSDictionary *data;
@property (nonatomic , weak) UILabel *warnLab;
@property (nonatomic , weak) UIImageView *imageView;
@property (nonatomic , weak) UIButton *wxButton;
@property(nonatomic, strong)UIVisualEffectView *effectView;

@end

@implementation SYJWXWarnView
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = RGBACOLOR(0, 0, 0, 0.5);
        self.frame = CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);
        
//        self.alpha  = 0.0f;
    }
    return self;
}

+ (SYJWXWarnView *)shareWXWarnView {
    static SYJWXWarnView *help = nil;
    static dispatch_once_t onceToken;
    dispatch_once(&onceToken, ^{
    
    
        help = [[SYJWXWarnView alloc] init];
        
        [[UIApplication sharedApplication].keyWindow addSubview:help];
        [help mas_makeConstraints:^(MASConstraintMaker *make) {
            make.center.equalTo([UIApplication sharedApplication].keyWindow);
            make.size.equalTo([UIApplication sharedApplication].keyWindow);
        }];
    });
    return help;
}
/**
 {
 id = 13;
 imgIndex = 3;
 imgUrl = "/files/2017-07-07/49737fc0-c998-4602-b96b-7d14df6baa23.jpg";
 linkUrl = "http://baidu.com";
 name = "\U6d4b\U8bd5\U6570\U636e001";
 }
 */
- (void)configTanChuangBannerData:(NSDictionary *)data{
    
    if ([JSLKitFactory isNotNull:data]) {
        self.data = data;
        NSURL *url =  [NSURL URLWithString:[NSString stringWithFormat:@"%@%@",Url_Server,data[@"imgUrl"]]];
        [self.backImageView sd_setImageWithURL:url placeholderImage:nil];
    }
}

- (void)WXWarnViewShowWithSuccess:(void (^)())success failure:(void (^)(NSError *))failure{
    
    if (self.backView == nil) {
        
        [self setUpAllChildView];
    }
    
    self.hidden = NO;
    [UIView animateWithDuration:0.75 delay:0 usingSpringWithDamping:0.5 initialSpringVelocity:1.0
                        options:UIViewAnimationOptionCurveEaseIn
                     animations:^{
                         self.effectView.effect = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
                         [self.backView.layer setValue:@(1.0) forKeyPath:@"transform.scale"];
                         
                         [self.canncelButton.layer setValue:@(1.0) forKeyPath:@"transform.scale"];
                     } completion:^(BOOL finished) {
                         
                         if (finished) {
                             if (success) {
                                 success();
                             }
                         }
                         
                     }];
   
}

- (void)WXWarnViewHideWithSuccess:(void (^)())success failure:(void (^)(NSError *))failure{
    
    self.hidden = YES;
    [UIView animateWithDuration:0.25 animations:^{
        [self.backView.layer setValue:@(0.0) forKeyPath:@"transform.scale"];
        [self.canncelButton.layer setValue:@(0.0) forKeyPath:@"transform.scale"];
//        self.effectView.effect = nil;
    } completion:^(BOOL finished) {
        
//        [self removeFromSuperview];
//        [self.backView removeFromSuperview];
//        self.backView = nil;
//        [self.canncelButton removeFromSuperview];
//        self.canncelButton = nil;
        
        if (finished) {
            if (success) {
                success();
            }
        }
    }];
    
//    [UIView animateWithDuration:0.5 delay:0.4 options:UIViewAnimationOptionCurveEaseOut animations:^{
//        self.alpha = 0.0f;
//        self.backView.alpha= 0.0f;
//        self.canncelButton.alpha = 0.0f;
//    } completion:^(BOOL finished) {
//        if (finished) {
//            if (success) {
//                success();
//            }
//        }else{
//            NSError *error = nil;
//            if (failure){
//                failure(error);
//            }
//        }
//    }];
}

- (void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{
    [self WXWarnViewHideWithSuccess:nil failure:nil];
}

- (void)buttonClick:(UIButton *)sender{
    
    [self WXWarnViewHideWithSuccess:nil failure:nil];
}

- (void)tapBannerAction{
    
    if ([self.WXWarnViewDelegate respondsToSelector:@selector(clickWXButtonAction:)]) {
        [self.WXWarnViewDelegate clickWXButtonAction:self];
    }
    
}
- (void)layoutSubviews{
    [super layoutSubviews];
    
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.mas_equalTo(SCREEN_WIDTH -36*AutoSizeScaleXX*2);
        make.height.mas_equalTo(220*AutoSizeScaleYY);
    }];
    
    
    self.backView.layer.mask = [JSLKitFactory roundedRect:CGRectMake(0, 0, SCREEN_WIDTH -36*AutoSizeScaleXX*2, 220*AutoSizeScaleYY) cornerRadius:10];
    
    [self.canncelButton mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.backView.mas_bottom).offset(29*AutoSizeScaleYY);
        make.centerX.equalTo(self.backView.mas_centerX);
        make.width.height.mas_equalTo(50*AutoSizeScaleXX);
        
    }];
    
 
    
    NSMutableParagraphStyle*style1 = [[NSMutableParagraphStyle alloc]init];
    [style1 setLineBreakMode:NSLineBreakByWordWrapping];
    [style1 setLineSpacing:2];
    NSDictionary*attribute1 =@{NSFontAttributeName:LIGHT_FONT(14),NSParagraphStyleAttributeName:style1};
    CGSize mainTitleSize1  = [self.warnLab.text boundingRectWithSize:CGSizeMake(SCREEN_WIDTH -36*AutoSizeScaleXX*2 -31*AutoSizeScaleXX*2 , MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin | NSStringDrawingUsesFontLeading attributes:attribute1 context:nil].size;
    
    self.warnLab.frame = CGRectMake(31*AutoSizeScaleXX, 25*AutoSizeScaleYY, mainTitleSize1.width, mainTitleSize1.height);
    
    NSRange range11 = [self.warnLab.text rangeOfString:@"叮当花花"];
    [JSLKitFactory attributedText:self.warnLab font:MEDIUM_FONT(14) range:range11 color:[UIColor blackColor]];
    
    self.imageView.frame = CGRectMake(self.warnLab.left, self.warnLab.bottom+12*AutoSizeScaleYY , self.warnLab.width, 120*AutoSizeScaleYY);
    
    self.wxButton.frame = CGRectMake(self.imageView.left, self.warnLab.bottom +11*AutoSizeScaleYY, self.imageView.width, 42*AutoSizeScaleYY);
    self.wxButton.layer.mask =
    
//    [JSLKitFactory roundedRect:self.wxButton.bounds roundingCorners:UIRectCornerAllCorners cornerRadius:0];
    [JSLKitFactory roundedRect:self.wxButton.bounds roundingCorners:UIRectCornerAllCorners cornerRadii:CGSizeMake(42*AutoSizeScaleXX*0.5, 42*AutoSizeScaleXX*0.5)];
//    332
    CGFloat height = (25+120+12+11+42+30)*AutoSizeScaleYY +mainTitleSize1.height;
    [self.backView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.mas_equalTo(SCREEN_WIDTH -36*AutoSizeScaleXX*2);
        make.height.mas_equalTo(@(height) );
    }];

    self.backView.layer.cornerRadius = 10;
    self.backView.layer.masksToBounds = YES;
//   self.backView.layer.mask = [JSLKitFactory roundedRect:CGRectMake(0, 0, SCREEN_WIDTH -36*AutoSizeScaleXX*2, height) cornerRadius:10];
}

- (void)setUpAllChildView{
    self.backView = [JSLKitFactory jsl_viewWithFrame:CGRectZero backgroundColor:[UIColor whiteColor] superView:self.superview];
    

    
    
    self.canncelButton = [JSLKitFactory jsl_imageButtonWithFrame:CGRectZero superView:self.superview setImage:@"XGroup" setBackImage:nil target:self action:@selector(buttonClick:)];
    


    NSString *text = @"公众号文本已经复制\n打开微信 - 点击搜索框 - 粘贴 - 搜一搜\n“叮当花花”\n就能关注\n不差钱的叮当花花公众号啦";


    
    self.warnLab = [JSLKitFactory jsl_labelWithFrame:CGRectZero text:text font:LIGHT_FONT(13) textColor:[@"#4a4a4a" hexStringToColor] textAlignment:NSTextAlignmentCenter SuperView:self.backView];
       self.warnLab.numberOfLines = 0;
    
    

    self.imageView = [JSLKitFactory jsl_imageViewWithFrame:CGRectZero imageName:@"syj_wx_image" superView:self.backView];
    
    
    
    self.wxButton = [JSLKitFactory jsl_titleButtonWithFrame:CGRectZero superView:self.backView font:LIGHT_FONT(18) titleColor:[UIColor whiteColor] backgroundColor:RGBCOLOR(68, 125, 255) title:@"马上去微信" target:self action:@selector(tapBannerAction)];
    
    [self.backView.layer setValue:@(0.0) forKeyPath:@"transform.scale"];
    
    [self.canncelButton.layer setValue:@(0.0) forKeyPath:@"transform.scale"];
}


- (UIVisualEffectView *)effectView
{
    if (!_effectView) {
        _effectView = [[UIVisualEffectView alloc]init];
        _effectView.effect = nil;
        _effectView.frame = self.backView.frame;
        _effectView.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    }
    return _effectView;
}
@end
